package com.rongshu.view.userInfo.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.penpal.adapter.PenpalListAdapter;
import com.rongshu.view.penpal.bean.PenpalResponse;
import com.rongshu.view.userInfo.iview.PenpalWillView;
import com.rongshu.view.userInfo.presenter.PenpalWillPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2019/3/19.
 * description：
 */
public class TaPenpalWillFragment extends BaseFragment<PenpalWillPresenter> implements PenpalWillView {
    @BindView(R.id.txt_top_title)
    TextView txtTopTitle;
    @BindView(R.id.penpalwill_recyclerView)
    RecyclerView penpalwillRecyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.layout_empty)
    RelativeLayout layoutEmpty;
    private List<PenpalResponse.ResponseBean> list;
    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private PenpalListAdapter mPenPalWillAdapter = null;
    private int pageIndex = 1;
    private int pageSize = 10;
    private int taId;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_pen_pal_will_view;
    }

    @Override
    protected void initViews() {
        txtTopTitle.setText("Ta的笔友会");
        list = new ArrayList<>();
        //manager
        manager = new LinearLayoutManager(getActivity());
        //adapter
        mPenPalWillAdapter = new PenpalListAdapter(getActivity(), list);
        //recyclerview
        penpalwillRecyclerView.setLayoutManager(manager);
        penpalwillRecyclerView.setAdapter(mPenPalWillAdapter);
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        taId = bundle.getInt(Constant.USER_ID);
        //set presenter
        setPresenter(new PenpalWillPresenter(getActivity()), this);
        //触发自动刷新
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                pageIndex = 1;
                smartRefreshLayout.resetNoMoreData();
                presenter.getTaJoinedPenpal(taId, pageIndex, pageSize);
            }
        });
        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getTaJoinedPenpal(taId, pageIndex, pageSize);
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void onPenpalWillMsg(List<PenpalResponse.ResponseBean> responseBeanList) {
        if (pageIndex == 1) {
            list.clear();
        }
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadmore();
        }
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
        list.addAll(responseBeanList);
        mPenPalWillAdapter.notifyDataSetChanged();
        layoutEmpty.setVisibility(list.size() == 0 ? View.VISIBLE : View.GONE);
        if (list.size() < pageSize) {
            smartRefreshLayout.setLoadmoreFinished(true);
        }
    }

    @OnClick(R.id.layout_back)
    public void onViewClicked() {
        getActivity().finish();
    }
}
